#!/usr/bin/env python3
from collections import defaultdict
import csv
import zd

date_set = set()
session = defaultdict(lambda :defaultdict(int))
create = {}

with open("/root/x.csv") as f:
    f = csv.reader(f)
    for pos,li in enumerate(f):
        if len(li)!=6:
            continue
        #if pos > 100000:
        #    break
        (androidId,phone,brand,lang,session_date,create_date) = li
        if not pos:
            continue

        session_date = session_date[:19]
        create_date = create_date[:19]

        if phone not in create:
            create[phone] = [create_date,brand,lang,androidId]

        d = session_date[:10]
        date_set.add(d)
        session[phone][d]+=1

date_li = list(sorted(date_set))

with zd.open("/root/phone.csv.zst","w") as out:
    f = csv.writer(out)
    f.writerow(["phone"]+date_li+"create_date,brand,lang,androidId".split(","))

    for user_id,session_date in sorted(
        create.items(),
        key=lambda x:x[1]
    ):
        li = [user_id]
        su = session[user_id]
        for i in date_li:
            li.append(su[i])
        f.writerow(li+create[user_id])
